﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace WUTOS.DataBase
{
    public partial class PeripheraAutoConfigFrame : Form
    {
        #region 参数 

        List<DataBaseDAL.BLL.databaseversion> databaseVersion =DataBaseDAL.BLL.GetLocalDataBaseVersion();

        //firewall
        private DataBaseDAL.FireWallControl fireControl = null;

        #endregion

        public PeripheraAutoConfigFrame()
        {
            InitializeComponent();
        }

        private void PeripheraAutoConfigFrame_Load(object sender, EventArgs e)
        {
            List<string> dbVersion = new List<string>();
            fireControl = new DataBaseDAL.FireWallControl();
            dbVersion.Add("");
            foreach ( DataBaseDAL.BLL.databaseversion Version in databaseVersion)
            {
                dbVersion.Add(Version.version);
            }
            foreach (string strName in dbVersion)
            {
                this.cbAutoServer.Items.Add(strName);
            }
            // 检测 mssql服务 
        }

        private void cbAutoServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ep.Clear();
            this.labTaskList.Text = "";
            if (string.IsNullOrEmpty(this.cbAutoServer.Text))
            {
                this.ep.SetError(this.cbAutoServer, "请选择服务器名称！");
                this.ActiveControl = cbAutoServer;
            }
            else
            {
                if (this.cbAutoServer.Text.Contains(@"Express"))  // express 服务器
                {
                    this.Cursor = Cursors.WaitCursor;
                    if (DataBaseDAL.BLL.OpenService(@"MSSQL$SQLEXPRESS"))
                    {
                        labTaskList.Text += "SQLEXPRESS服务已经启动 ";
                        if (DataBaseDAL.BLL.OpenService(@"SQLBrowser"))
                        {
                            labTaskList.Text += "\r\nSQLBrowser服务已经启动 ";
                            string filePath = string.Empty;
                            if (this.cbAutoServer.Text.Contains("2005"))//2005 express
                            {
                                filePath = @"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn";
                            }
                            if (this.cbAutoServer.Text.Contains("2008"))//2008 express
                            {
                                filePath = @"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn";
                            }
                            filePath = Path.Combine(filePath, @"sqlservr.exe");
                            if (File.Exists(filePath))
                            {
                                fireControl.AddApplication(filePath, @"sqlservr.exe");
                                labTaskList.Text += "\r\nsqlservr.exe添加防火墙例外成功";

                                DataBaseDAL.BLL.SetRemotConnection(this.cbAutoServer.Text, 1, 1);
                                labTaskList.Text += "\r\ntcp ip name pipes 协议启用";
                            
                                //确定启动服务的类型：true：MSSQLSERVER，false：SQLEXPRESS
                                bool type = this.cbAutoServer.Text.Contains("Developer") ? true : false;
                                bool ret = DataBaseDAL.BLL.RestartSqlserver(type);
                                if (ret)
                                {
                                    MessageBox.Show(this.cbAutoServer.Text+"服务自动配置成功！","系统提示",
                                         MessageBoxButtons.OK,MessageBoxIcon.Information);
                                }
                                else
                                {
                                    MessageBox.Show(this.cbAutoServer.Text + "服务自动配置失败！", "系统提示",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                            else
                            {
                                labTaskList.Text += "\r\nsqlservr.exe添加防火墙例外失败！";
                                labTaskList.Text += "\r\n路径访问失败！";
                            }
                        }
                        else
                        {
                            labTaskList.Text += "\r\nSQLBrowser服务启动失败！";
                        }
                    }
                    else
                    {
                        labTaskList.Text += "SQLEXPRESS服务启动失败！";
                    }
                    this.Cursor = Cursors.Arrow;
                }
                else //develop 服务器
                {
                    if (DataBaseDAL.BLL.OpenService(@"MSSQLSERVER"))
                    {
                        this.Cursor = Cursors.WaitCursor;
                        labTaskList.Text += "MSSQLSERVER服务已经启动";
                        if (DataBaseDAL.BLL.OpenService(@"SQLBrowser"))
                        {
                            labTaskList.Text += "\r\nSQLBrowser服务已经启动";
                            string filePath = string.Empty;
                            if (this.cbAutoServer.Text.Contains("2005"))//sql 2005 develop
                            {
                                filePath = @"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn";
                            }
                            if (this.cbAutoServer.Text.Contains("2008"))//sql 2008 develop
                            {
                                filePath = @"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn";
                            }
                            filePath = Path.Combine(filePath, @"sqlservr.exe");
                            if (File.Exists(filePath))
                            {
                                fireControl.AddApplication(filePath, @"sqlservr.exe");
                                labTaskList.Text += "\r\nsqlservr.exe添加防火墙例外成功";

                                DataBaseDAL.BLL.SetRemotConnection(this.cbAutoServer.Text, 1, 1);
                                labTaskList.Text += "\r\ntcp ip name pipes 协议启用";
                                //确定启动服务的类型：true：MSSQLSERVER，false：SQLEXPRESS
                                bool type = this.cbAutoServer.Text.Contains("Developer") ? true : false;
                                bool ret = DataBaseDAL.BLL.RestartSqlserver(type);
                                if (ret)
                                {
                                    MessageBox.Show(this.cbAutoServer.Text + "服务自动配置成功！", "系统提示",
                                         MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                {
                                    MessageBox.Show(this.cbAutoServer.Text + "服务自动配置失败！", "系统提示",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                            else
                            {
                                labTaskList.Text += "\r\nsqlservr.exe添加防火墙例外失败！";
                                labTaskList.Text += filePath+"访问目录失败！";
                            }

                        }
                        else
                        {
                            labTaskList.Text += "\r\nSQLBrowser服务启动失败！";
                        }
                    }
                    else
                    {
                        labTaskList.Text += "MSSQLSERVER服务启动失败！";
                    }
                    this.Cursor = Cursors.Arrow;
                }
            }
        }
    }
}
